﻿
var G_vmlCanvasManager;
var canvas;
var ctx;   
var keys = {37: ["4",0,'left'], 38: ["2",0,'up'], 39: ["0",0,'right'], 40: ["1",0,'down'], 32: ["3",0,'fire']};
var wsOpen = 0;

var curPlayer = -1
function startCanvas() {        	
    // NOP
}

function webSocketOpen() {
  if ("WebSocket" in window)
  {
     // Let us open a web socket
     //setInterval(Delay, 400);
     ws = new WebSocket("ws://s3f.ru:10401/echo");
     ws.onopen = function() {
        // Web Socket is connected, send data using send()
	document.getElementById('test-id2').innerHTML = "opened";
	wsOpen = 1;
     };
	 
     ws.onmessage = Received;
     ws.onclose = function()
     { 
        // websocket is closed.
        alert("Соединение прервано"); 
     };
  }
  else {
     // The browser doesn't support WebSocket
     alert("WebSocket NOT supported by your Browser!");
  }
}
 $(document).ready(function(){
	
	canvas = document.getElementById('canvas');
	// Проверяем для IE
    if (G_vmlCanvasManager != undefined)
        G_vmlCanvasManager.initElement(canvas);
            
    // Проверяем понимает ли браузер canvas
    if (canvas.getContext) {
        ctx = canvas.getContext('2d'); // Получаем 2D контекст
        	
	}
	policeX = canvas.width/2;
	policeY = canvas.height/2;
	alert("ready?");
	webSocketOpen();
	//while(curPlayer == -1) { };
	OwnPlayer = new Player(1,canvas);
	OtherPlayer = new Player(0,canvas);
});

function WebSocketSend(data) {
		if(wsOpen)
			ws.send(data);
        //alert("Message is sent...");
}



var currXindex = 10;
var currYindex = 10;
var maxX = 40;
var maxY = 40;

	gridX = 10;
	gridY = 10;


state = new Array();
    for(i=1;i<=maxX;i++) {
        state[i] = new Array();
        for(j=1;j<=maxY;j++) {
			state[i][j]=0;
        }
    }
	
	
var duration = 5000;
var start = new Date().getTime();

function collision(_x1,_y1,_x2,_y2,_thres) {
	var r = (Math.pow((_x1 - _x2),2) + Math.pow((_y1 - _y2),2));
	if (r < (_thres * _thres) ) 
	{
		return true;
	}
	return false;
}